package net.dodoman.thread;

public class JoinThread extends Thread {
    public static int n = 0;

    static synchronized void inc() {
	n++;
    }

    public void run() {
	for (int i = 0; i < 10; i++)
	    try {
		inc();
		// 为了使运行结果更随机，延迟3毫秒
		sleep(3);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
    }

    public static void test() throws Exception {
	JoinThread threads[] = new JoinThread[100];
	for (int i = 0; i < threads.length; i++) {
	    threads[i] = new JoinThread();
	    threads[i].start();
	    //threads[i].join();
	}
	System.out.println("n=" + JoinThread.n);

    }

    public static void main(String args[]) {
	try {
	    test();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
}
